package com.iflytek.uoamp.agent.common.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;


public class ReadFileByLine {
	
	public static List<String> getAllLine2Array(String file,String encoding,int skipNum,int readNum) {
		ArrayList<String> res = new ArrayList<String>();
		BufferedReader readConf = null;
		FileInputStream in = null;
		InputStreamReader redar = null;
		try {
			in = new FileInputStream(file);
			redar = new InputStreamReader(in,encoding);
			readConf = new BufferedReader(redar);
			String line;
			int lineNum = 0;
			int rnum = 0;
			while (null != (line = readConf.readLine())) {
				if (!line.startsWith("#") && !"".equals(line.trim())) {
					lineNum++;
					if(lineNum>skipNum){
						if(readNum==-1 || rnum<readNum){
							rnum++;
							res.add(line);
						}else{
							break;
						}
					}
				}
			}
		} catch (Exception e) {
			Logger.getRootLogger().error(e.getMessage(),e);
		}finally{
			try {
				if(readConf!=null){
					readConf.close();
				}
			} catch (IOException e) {
				Logger.getRootLogger().error(e.getMessage(),e);
			}
			try {
				if(redar!=null){
					redar.close();
				}
			} catch (IOException e) {
				Logger.getRootLogger().error(e.getMessage(),e);
			}
			try {
				if(in!=null){
					in.close();
				}
			} catch (IOException e) {
				Logger.getRootLogger().error(e.getMessage(),e);
			}
		}
		return res;
	}

	public static List<String> getAllLine2Array(String file) {
		return getAllLine2Array(file,"utf-8",0,-1);
	}
	
}
